package com.lry.util.requestcore.handler;

import com.lry.util.requestcore.handler.parse.JsonParser;
import com.lry.util.requestcore.handler.parse.XmlParser;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据解析器
 *
 * @author ALI
 * @since 2023/5/8
 */
public abstract class StringParser {

    public static List<Map<String, Object>> parse(String data) {
        StringParser select = select(data);
        return select.parseList(data);
    }

    private static StringParser select(String data) {
        if (StringUtils.isBlank(data)) {
            throw new RuntimeException("数据格式解析器 - 没有匹配到适合的解析器！");
        }
        data = data.trim();
        if (data.startsWith("<html")) {
            return null;
        } else if (data.startsWith("{") || data.startsWith("[")) {
            return new JsonParser();
        } else if (data.startsWith("<soap")) {
            return new XmlParser();
        }
        return null;
    }

    protected abstract Object parserObj(String data);

    protected abstract List<Map<String, Object>> parseList(String data);

}
